//
//  UIBarButtonItem+Extension.swift
//  HHWeibo
//
//  Created by hehuafeng on 2017/2/16.
//  Copyright © 2017年 雷厉峰行. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    
    /// 快速创建自定义UIBarButtonItem
    ///
    /// - Parameters:
    ///   - title: title
    ///   - fontSize: fontSize 默认“16号字体”
    ///   - normalColor: 默认文字颜色
    ///   - highlightedColor: 高亮文字颜色
    ///   - target: target
    ///   - action: action
    convenience init(title: String, fontSize: CGFloat = 16, normalColor: UIColor, highlightedColor: UIColor, target: Any?, action: Selector, isBack: Bool = false) {
        // 1. 创建按钮
        let btn: UIButton = UIButton()
        
        btn.setTitleColor(normalColor, for: .normal)
        btn.setTitleColor(highlightedColor, for: .highlighted)
        
        btn.setTitle(title, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        
        btn.sizeToFit()
        
        // 2. 监听按钮
        btn.addTarget(target, action: action, for: .touchUpInside)
        
        // 3. 设置返回按钮箭头
        if isBack {
            btn.setImage(UIImage(named: "hotweibo_back_icon"), for: .normal)
            btn.setImage(UIImage(named: "hotweibo_back_icon_highlighted"), for: .highlighted)
            
            // 按钮自适应内容的大小, 否则, 图片将会被挤压
            btn.sizeToFit()
        }
        
        // 4. 实例化 UIBarButtonItem
        self.init(customView: btn)
    }
}
